package com.zpkj.portal.service.user.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.zpkj.portal.dao.mapper.UserMainMapper;
import com.zpkj.portal.entity.UserMain;
import com.zpkj.portal.service.user.UserMainService;
import com.zpkj.portal.utils.Identities;
import com.zpkj.portal.utils.Md5Utils;

import tk.mybatis.mapper.entity.Example;

/**
 * @author xc
 */
@Service
public class UserMainServiceImpl implements UserMainService {

    //@Resource
    //private EnvironmentConfig environmentConfig;
    @Autowired
    private UserMainMapper userMainMapper;
    
    /* (non-Javadoc)
	 * @see com.zpkj.portal.service.user.UserMainService#add(com.zpkj.portal.entity.UserMain)
	 */
    @Override
	public int add(UserMain userMain) {
        return userMainMapper.insert(userMain);
    }
    
    @Override
   	public int addTest() {
    	UserMain userMain = new UserMain();
    	userMain.setUserId(Identities.uuid());
    	userMain.setUserCode("123456");
    	userMain.setNickName("nn");
    	userMain.setUserName("un");
    	userMain.setPassword(Md5Utils.encryption("162534"));
    	userMain.setUserPhone("13512344321");
    	userMain.setCreateTime(new Date());
    	userMain.setVersion(1);
		return userMainMapper.insert(userMain);
	}

    /* (non-Javadoc)
	 * @see com.zpkj.portal.service.user.UserMainService#getUserMainById(java.lang.String)
	 */
    @Override
	public UserMain getUserMainById(String id){
    	return userMainMapper.selectByPrimaryKey(id);
    }
    
    /* (non-Javadoc)
	 * @see com.zpkj.portal.service.user.UserMainService#getUserIdByUserTel(java.lang.String)
	 */
    @Override
	public UserMain getUserIdByUserTel(String tel){
    	Example example = new Example(UserMain.class);
    	example.createCriteria().andEqualTo("userPhone", tel);
    	List<UserMain> userMainList = userMainMapper.selectByExample(example);
    	if(userMainList.size()>0){
    		return userMainList.get(0);
    	}
    	return null;
    }

    /* (non-Javadoc)
	 * @see com.zpkj.portal.service.user.UserMainService#getUserIdByTel(java.lang.String)
	 */
    @Override
	public UserMain getUserIdByTel(String tel) {
        Map<String, String> map = new HashMap<String, String>(1);
        map.put("userPhone", tel);
        UserMain userMain = userMainMapper.getUserMain(map);
        return userMain;
    }

    /* (non-Javadoc)
	 * @see com.zpkj.portal.service.user.UserMainService#getUserByUserCode(java.lang.String)
	 */
    @Override
	public UserMain getUserByUserCode(String userCode) {
        Map<String, String> map = new HashMap<String, String>(1);
        map.put("userCode", userCode);
        UserMain userMain = userMainMapper.getUserMain(map);
        return userMain;
    }

    /* (non-Javadoc)
	 * @see com.zpkj.portal.service.user.UserMainService#getUserMainByUserIds(java.lang.String)
	 */
    @Override
	//@Cacheable(value = "userMainCache", key = "'userMain_by_userId_'+#userId")
    public UserMain getUserMainByUserIds(String userId) {
        Map<String, String> map = new HashMap<String, String>(1);
        map.put("userId", userId);
        UserMain userMain = userMainMapper.getUserMain(map);
        /*
        UserMain record = new UserMain();
        record.setUserId(userId);
        List<UserMain> list = userMainMapper.select(record);
        UserMain userMain = null;
        if(list != null) {
        	userMain = list.get(0);
        }
        */
        
        return userMain;
    }

    /* (non-Javadoc)
	 * @see com.zpkj.portal.service.user.UserMainService#getUserMainById(java.lang.String)
	 */
    @Override
	public UserMain getUserMainByUserId(String userId) {
        //redisService.expire("userMain_by_userId_"+userId,3600);
        return getUserMainByUserIds(userId);
    }

    /* (non-Javadoc)
	 * @see com.zpkj.portal.service.user.UserMainService#existUser(java.lang.String)
	 */
    @Override
	public boolean existUser(String userId) {
        Map<String, String> map = new HashMap<String, String>(1);
        map.put("userId", userId);
        UserMain userMain = userMainMapper.getUserMain(map);
        if (userMain != null) {
            return true;
        }
        return false;
    }

    /* (non-Javadoc)
	 * @see com.zpkj.portal.service.user.UserMainService#updateUser(com.zpkj.portal.entity.UserMain)
	 */
    @Override
	public int updateUser(UserMain userMain) {
        userMain.setUpdateTime(new Date());
        Example example = new Example(UserMain.class);
        example.createCriteria().andEqualTo("id", userMain.getId());
        int num = userMainMapper.updateByExampleSelective(userMain, example);
        if (num == 0) {
            num = userMainMapper.updateByExampleSelective(userMain, example);
        }
        return num;
    }

}
